<!DOCTYPE html>
<html>
<script src="../../../../resources/js-test.js"></script>
<link rel="stylesheet" href="../resources/touch-hover-active-tests.css">
<style type="text/css">
#box {
    width: 300px;
    height: 100px;
}
</style>
<body>
<div id="box" class="touch-interactive">Gestures go here</div>

<p id="description"></p>
<p>See https://bugs.webkit.org/show_bug.cgi?id=96060 for details</p>

<div id="console"></div>

<script src="../resources/touch-hover-active-tests.js"></script>
<script>
var box = document.getElementById("box");
description("Tests that tap gesture events set and clear the active state of elements.");

function runTests()
{
    if (!window.eventSender) {
        debug('This test requires DRT.');
        return;
    }

    if (!eventSender.gestureShowPress) {
        debug('GestureShowPress is not supported by this platform');
        return;
    }

    if (window.testRunner) {
        window.jsTestIsAsync = true;
        testRunner.waitUntilDone();
    }

    debug("Verify active isn't initially set");
    shouldBeDefault("getHoverActiveState(box)");

    debug("Verify showPress, tap sets and clears active");
    eventSender.gestureTapDown(50, 50);
    eventSender.gestureShowPress(50, 50);
    shouldBeHoveredAndActive("getHoverActiveState(box)");
    eventSender.gestureTap(50, 50);
    // FIXME: Enable after implementing mocked timers (http://crbug.com/319529)
    // shouldBeHoveredAndActive("getHoverActiveState(box)");
    waitUntilActiveCleared();
}

function waitUntilActiveCleared()
{
    if (getHoverActiveState(box) == "hoveredAndActive") {
        return setTimeout(waitUntilActiveCleared, 10);
    }

    shouldBeOnlyHovered("getHoverActiveState(box)");

    debug("Verify showPress, tapCancel on the element sets and clears active");
    eventSender.gestureTapDown(50, 50);
    eventSender.gestureShowPress(50, 50);
    shouldBeHoveredAndActive("getHoverActiveState(box)");
    eventSender.gestureTapCancel(50, 50);
    shouldBeOnlyHovered("getHoverActiveState(box)");

    debug("Verify tap elsewhere still clears active");
    eventSender.gestureTapDown(50, 50);
    eventSender.gestureShowPress(50, 50);
    shouldBeHoveredAndActive("getHoverActiveState(box)");
    eventSender.gestureTap(400, 250);
    // FIXME: Remove after mocked timers added  (http://crbug.com/319529)
    waitUntilAnotherActiveCleared();
}

function waitUntilAnotherActiveCleared()
{
    if (getHoverActiveState(box) == "hoveredAndActive") {
        return setTimeout(waitUntilAnotherActiveCleared, 10);
    }

    shouldBeDefault("getHoverActiveState(box)");

    debug("Verify tapCancel elsewhere still clears active");
    eventSender.gestureTapDown(50, 50);
    eventSender.gestureShowPress(50, 50);
    shouldBeHoveredAndActive("getHoverActiveState(box)");
    eventSender.gestureTapCancel(400, 250);
    shouldBeDefault("getHoverActiveState(box)");

    debug("Verify that touchStart doesn't trigger active state");
    eventSender.addTouchPoint(50, 50);
    eventSender.touchStart();
    shouldBeDefault("getHoverActiveState(box)");
    eventSender.releaseTouchPoint(0);
    eventSender.touchEnd();

    debug("Verify that touchStart/touchEnd doesn't cancel active");
    eventSender.gestureTapDown(50, 50);
    eventSender.gestureShowPress(50, 50);
    shouldBeHoveredAndActive("getHoverActiveState(box)");
    eventSender.addTouchPoint(50, 50);
    eventSender.touchStart();
    shouldBeHoveredAndActive("getHoverActiveState(box)");
    eventSender.releaseTouchPoint(0);
    eventSender.touchEnd();
    shouldBeHoveredAndActive("getHoverActiveState(box)");
    eventSender.gestureTapCancel(50, 50);
    shouldBeOnlyHovered("getHoverActiveState(box)");

    finishJSTest();
}

window.onload = runTests;
</script>
</body>
</html>
